package com.letv.ads.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.letv.adlib.managers.status.ad.AdStatusManager;
import com.letv.adlib.sdk.types.AdElement;
import com.letv.adlib.sdk.types.AdElementMime;
import com.letv.ads.util.AdsUtils;
import com.letv.ads.util.LogInfo;
import com.letv.core.download.image.ImageDownloadStateListener;
import com.letv.core.download.image.ImageDownloader;

/**
 * 广告图片控件，主要用于显示 开机广告，焦点图广告，banner广告，搜索大图广告
 */
public class AdImgaeView extends ImageView implements IAdView {
    private static final String TAG = "AdImgaeView";
    private AdMaterialLoadListener mAdMaterialLoadListener;
    private AdViewOnclickListenr mAdViewOnclickListenr;
    private AdPlayStateListener mAdPlayStateListener;
    private AdElementMime mAdInfo;
    private boolean mHasClosed = false;

    public AdImgaeView(Context context) {
        super(context);
    }

    public AdImgaeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void showAD(final AdElementMime adInfo) {
        if (adInfo != null) {
            mAdInfo = adInfo;
            if (AdElement.AdMediaType.BITMAP != adInfo.mediaFileType) {
                return;
            }
            ImageDownloader.getInstance(getContext()).download(adInfo.mediaFileUrl, new ImageDownloadStateListener() {
                @Override
                public void loading() {

                }

                @Override
                public void loadSuccess(Bitmap bitmap) {
                    AdImgaeView.this.setImageBitmap(bitmap);
                    boolean isComplete = true;
                    if (mAdMaterialLoadListener != null) {
                        isComplete = mAdMaterialLoadListener
                            .onLoadComplete();
                    }
                    if (null != mAdPlayStateListener) {
                        Bundle bundle = new Bundle();
                        bundle.putInt(KEY_STATE, PLAY_STATE_STARTED);
                        mAdPlayStateListener
                            .onADPlayStateChange(bundle);
                    }
                    LogInfo.log(TAG, "onLoadingComplete isComplete="
                        + isComplete);
                    // 混合非播放器内广告控制重复曝光，其他广告不控制
                    if (isComplete
                        && (!adInfo.hadExposed || !adInfo
                        .isComplexAD())) {
                        /**
                         * 曝光统计
                         * */
                        AdStatusManager statusMgr = new AdStatusManager(
                            adInfo);
                        statusMgr.onAdPlayStart();
                    }
                    setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (null != mAdViewOnclickListenr
                                && AdsUtils.checkClickEvent()) {
                                mAdViewOnclickListenr
                                    .onADClick(mAdInfo);
                            }
                        }
                    });
                }

                @Override
                public void loadFailed() {
                    if (mAdMaterialLoadListener != null) {
                        mAdMaterialLoadListener.onLoadFailed();
                    }
                }
            });

        }
    }

    @Override
    public void closeAD() {
        if (mHasClosed) {
            return;
        } else {
            mHasClosed = true;
        }
        setVisibility(GONE);
        if (null != mAdInfo) {
            /**
             * 完成统计
             * */
            AdStatusManager statusMgr = new AdStatusManager(mAdInfo);
            statusMgr.onAdPlayComplete();
        }
    }

    @Override
    public void setAdOnClickListener(AdViewOnclickListenr adViewOnclickListenr) {
        mAdViewOnclickListenr = adViewOnclickListenr;
    }

    @Override
    public void setAdMaterialLoadListener(
            AdMaterialLoadListener adMaterialLoadListener) {
        mAdMaterialLoadListener = adMaterialLoadListener;
    }

    @Override
    public void setAdPlayStateListener(AdPlayStateListener adPlayStateListener) {
        mAdPlayStateListener = adPlayStateListener;
    }

}
